package com.gzs.learn.springcloud.serialize;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.time.DateUtils;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

public class CustomeDateDeserializer extends JsonDeserializer<Date> {
    private static String[] ACCEPT_PATTERNS = {"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"};
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        String dateStr = jsonParser.getText().trim();
        try {
            return DateUtils.parseDate(dateStr,ACCEPT_PATTERNS);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(dateStr);
        }
    }
}
